package br.inf.solutions.apresentacoes.ssp.control.hierarchy;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.basics.BasicFormsController;
import br.inf.solutions.apresentacoes.ssp.model.PostGraduation;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.HierarchyStructEntity;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.HierarchyStructType;

@Service
@Scope("prototype")
public class HierarchyStructEntityController extends BasicFormsController<HierarchyStructEntity>{

	public HierarchyStructEntityController() {
		super(HierarchyStructEntity.class);
	}
	
	public Return getAllByType(){
		HierarchyStructType type = null;
		String typeName = null;
		if(data.get("hierarchyType") instanceof String){
			typeName = (String) data.get("hierarchyType");
		} else {
			type = (HierarchyStructType) data.get("hierarchyType");
		}
		
		String sql = "FROM HierarchyStructEntity WHERE type = " + type.getId() + " or type.name =" + typeName; 
		
		data.put("sql", sql);		
		return searchByHQL();
	}
	
	protected HierarchyStructType getHierarchyType(String type){
		HierarchyStructTypeController controller = SpringFactory.getController("hierarchyStructTypeController", HierarchyStructTypeController.class, null);
		HierarchyStructType t = null;
		for(Object h : controller.listAll().getList()){
			HierarchyStructType hy = (HierarchyStructType) h;
			if(hy.getName().equals(type)){
				t = hy;
			}
		}
		return t;
	}	
	
	public List<HierarchyStructEntity> getHierarchyEntitysByType(HierarchyStructEntity father, String type, List<HierarchyStructEntity> previousList){
		getPersistence().getSession().refresh(father);
		if (previousList == null){
			previousList = new ArrayList<>();
		}
		if(father.getType().getName().equals(type)){
			if (!previousList.contains(father)){
				previousList.add(father);
			}
		}
		if (father.getChildrens() != null && !father.getChildrens().isEmpty()){
			for(HierarchyStructEntity h : father.getChildrens()){
				previousList = getHierarchyEntitysByType(h, type, previousList);
			}
		}
		return previousList;
	}
	
	@SuppressWarnings("unchecked")
	public Return getListPosts(){
		Return ret = new Return(true);
		List<Long> ids =  (List<Long>) data.get("listaID");			
		String sql = "SELECT COUNT(pg.name), pg.name,pg.id FROM Person AS p, PostGraduation AS pg WHERE p.post.id=pg.id AND p.unity in (" +  ids.toString().substring(1,ids.toString().length()-1)  +") group by pg.id,pg.name ORDER BY pg.id ASC";		
		data.put("sql", sql);		
		ret.concat(searchByHQL());
		return ret;
	}
	
	@SuppressWarnings("unchecked")
	public Return getListWeapons(){
		Return ret = new Return(true);	
		List<Long> ids = (List<Long>) data.get("listaID");	
		String sql = "SELECT COUNT(w.typeWeapon.name), w.typeWeapon.name FROM Weapon AS w WHERE w.unity in (" +  ids.toString().substring(1,ids.toString().length()-1)  +") group by w.typeWeapon.name";
		data.put("sql", sql);		
		ret.concat(searchByHQL());
		return ret;
	}
	
	@SuppressWarnings("unchecked")
	public Return getListPersons(){
		Return ret = new Return(true);
		List<Long> ids = (List<Long>) data.get("listaID");	
		String sql = "FROM Person WHERE unity in (" +  ids.toString().substring(1,ids.toString().length()-1)  +")";
		data.put("sql", sql);		
		ret.concat(searchByHQL());
		return ret;
	}
	
	@SuppressWarnings("unchecked")
	public Return getRetirement(){
		Return ret = new Return(true);
		List<Long> ids =  (List<Long>) data.get("listaID");	
		PostGraduation post = (PostGraduation) data.get("selectedPost");
		String sql = "FROM Person WHERE post.id = '" +  post.getId() + "' AND unity.id in (" +  ids.toString().substring(1,ids.toString().length()-1)  +")"; 
		data.put("sql", sql);
		ret.concat(searchByHQL());
		return ret;
	}	
}